import React from 'react';
import { Form, Icon, Input, Button, Checkbox } from 'antd';
import http from '../utils/api';
import './Login.css';
import { withRouter } from 'react-router-dom';

const FormItem = Form.Item;


class Login extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      message: '',
    };
  }
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        http.login(values, teacher => {
          console.log(teacher);
          this.props.history.push('/admin')
        }, err => {
          if (err.status === 401) {
            alert('账号和密码不匹配');
          }
        });
      }
    });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <div style={{ display: 'flex', justifyContent: 'center', paddingTop: '3rem' }}>
        <Form onSubmit={this.handleSubmit} className="login-form">
          <FormItem>
            {getFieldDecorator('mobile', {
              rules: [{ required: true, message: '请输入手机号!' }],
            })(
              <Input prefix={<Icon type="mobile" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="手机号" />
            )}
          </FormItem>
          <FormItem>
            {getFieldDecorator('password', {
              rules: [{ required: true, message: '请输入密码!' }],
            })(
              <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="密码" />
            )}
          </FormItem>
          <FormItem>
            {getFieldDecorator('remember', {
              valuePropName: 'checked',
              initialValue: true,
            })(
              <Checkbox>记住我</Checkbox>
            )}
            <a className="login-form-forgot" href="">忘记密码</a>
            <Button type="primary" htmlType="submit" className="login-form-button">
              登录
                        </Button>
          </FormItem>
        </Form>
      </div>
    );
  }
}
export default Form.create()(withRouter(Login));